﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DAO_ADMIN;
using PTTK19.DB;

namespace PTTK19.DB
{
    public class DBThucDon
    {
        internal static DataTable GetList()
        {
            //DBMS dbms = new DBMS();
            string procname = "sp_LayDanhSachThucDon";
            DataTable dt = DataProvider.SelectStoreProcedure(procname);
            return dt;
        }


        internal static bool AddThucDon(string _TenMonAn, int _TinhTrang)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> parr = new List<SqlParameter>();
                string procname = "sp_ThemThucDon";
                parr.Add(new SqlParameter("@TenMonAn", _TenMonAn));
                parr.Add(new SqlParameter("@CachCheBien", _TinhTrang));
                DataProvider.NonqueryProc(procname, parr);
                kq = true;
            }
            catch (Exception)
            {
            }
            return kq;
        }

        internal static bool UpdateThucDon(int _MaThucDon, string _TenThucDon, int _TinhTrang)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> parr = new List<SqlParameter>();
                string procname = "sp_UpdateThucDon";
                parr.Add(new SqlParameter("@MaThucDon", _MaThucDon));
                parr.Add(new SqlParameter("@TenThucDon", _TenThucDon));
                parr.Add(new SqlParameter("@TinhTrang", _TinhTrang));
                DataProvider.NonqueryProc(procname, parr);
                kq = true;
            }
            catch (Exception)
            {
            }
            return kq;
        }

        internal static bool DelThucDon(int _MaThucDon)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> parr = new List<SqlParameter>();
                string procname = "sp_DelThucDon";
                parr.Add(new SqlParameter("@MaThucDon", _MaThucDon));
                DataProvider.NonqueryProc(procname, parr);
                kq = true;
            }
            catch (Exception)
            {
            }
            return kq;
        }

        internal static DataTable GetListID()
        {
            string procname = "sp_DSMaThucDonTonTai";
            DataTable dt = DataProvider.SelectStoreProcedure(procname);
            return dt;
        }

        public static void ThemThucDon(string tenthucdon, DateTime ngaylap)
        {
            List<SqlParameter> parr = new List<SqlParameter>();
            string procname = "ThemThucDon";
            parr.Add(new SqlParameter("@tenthucdon", tenthucdon));
            parr.Add(new SqlParameter("@ngaylap", ngaylap));
            DataProvider.NonqueryProc(procname, parr);
        }

        public static int LayMaxMaThucDon()
        {
            DBMS dbms = new DBMS();
            dbms.Connect();
            string sql = "select Max(MaThucDon) from ThucDon";
            int i = (int)dbms.ExecuteScalar(sql);
            dbms.Disconnect();
            return i;
        }

        public static int ThucDonCuaNgay(string ngay)
        {
            int i;
            DBMS dbms = new DBMS();
            dbms.Connect();
            string sql = "select MaThucDon from ThucDon where NgayLap = '" + ngay + "'";
            if (dbms.ExecuteScalar(sql) != null)
            {
                i = (int)dbms.ExecuteScalar(sql);
            }
            else
            {
                i = -1;
            }
            dbms.Disconnect();
            return i;
        }
    }
}
